package com.benio.ycfdisplayer;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.benio.ycfdisplayer.displayer.DisplayListener;
import com.benio.ycfdisplayer.displayer.Options;
import com.benio.ycfdisplayer.displayer.SimpleDisplayListener;
import com.benio.ycfdisplayer.displayer.YcfDisplayer;
import com.benio.ycfdisplayer.fresco.FrescoDisplayOptions;

public class FrescoExecutorActivity extends AppCompatActivity {
    private static final String TAG = "FrescoDisplayerActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 先初始化，使用SimpleDraweeView的话要在setContentView之前调用
        YcfDisplayer.initialize(this);
        setContentView(R.layout.activity_fresco_executor);

        String uri1 = "http://a2.att.hudong.com/71/04/300224654811132504044925945_950.jpg";
        // 渐变jpeg
        String uri2 = "http://pooyak.com/p/progjpeg/jpegload.cgi?o=1";
        String uri3 = "http://img2.3lian.com/2014/f5/63/d/16.jpg";
        View view1 = findViewById(R.id.image1);
        View view2 = findViewById(R.id.image2);
        View view3 = findViewById(R.id.image3);

        YcfDisplayer displayer = YcfDisplayer.get();
        Options options = new Options.Builder()
                .setFailureImage(new ColorDrawable(Color.RED))
                .setPlaceHolderImage(new ColorDrawable(Color.GREEN))
                .build();
        FrescoDisplayOptions frescoOptions = new FrescoDisplayOptions.Builder()
                .setFadeDuration(500)
                .setFailureImage(new ColorDrawable(Color.RED))
                .setPlaceHolderImage(new ColorDrawable(Color.BLUE))
                .setTapToRetryEnabled(true)
                .setRetryImage(ContextCompat.getDrawable(this, R.mipmap.ic_launcher))
                .setDiskCacheEnabled(false)
                .setProgressiveRenderingEnabled(true)
                .build();
        DisplayListener displayListener = new SimpleDisplayListener() {
            @Override
            public void onStart(String imageUri, View view) {
                Log.d(TAG, "onStart() called with: imageUri = [" + imageUri + "], view = [" + view + "]");
            }

            @Override
            public void onSuccess(String imageUri, View view, Object loadedData) {
                Log.d(TAG, "onSuccess() called with: imageUri = [" + imageUri + "], view = [" + view + "], loadedData = [" + loadedData + "]");
            }

            @Override
            public void onFailure(String imageUri, View view, Throwable throwable) {
                Log.d(TAG, "onFailure() called with: imageUri = [" + imageUri + "], view = [" + view + "], throwable = [" + throwable + "]");
            }
        };

        displayer.display(uri1).options(options).listener(displayListener).into(view1);
        displayer.display(uri2).options(frescoOptions).listener(displayListener).into(view2);
        displayer.display(uri3).listener(displayListener).into(view3);
    }
}
